﻿package com.mui.manager
{
    import com.gengine.global.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.mui.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class DragManager extends Object
    {
        public var currentDragItem:IDragDrop;
        public var currentDrogItem:IDragDrop;
        private var dragLayer:DisplayObjectContainer;
        private var tempDragItem:DisplayObject;
        private var thumItem:Sprite;
        private var oldMoveOverItem:IDragDrop;
        private var tempCurrentDragItem:IDragDrop;
        private var currentDragSource:Object;
        private var dragBitMapData:BitmapData;
        private static var _instance:DragManager;

        public function DragManager(param1:DisplayObjectContainer)
        {
            if (_instance != null)
            {
                throw new Error(" DragManager 单例 ");
            }
            this.dragLayer = param1;
            _instance = this;
            return;
        }// end function

        public function startDragItem(param1:IDragDrop, param2:BitmapData = null) : void
        {
            var _loc_4:Rectangle = null;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            if (!param1.isDragAble)
            {
                return;
            }
            this.dragBitMapData = param2;
            this.currentDragSource = param1.dragSource;
            var _loc_3:* = param1 as DisplayObject;
            if (instance.tempCurrentDragItem == null)
            {
                _loc_4 = _loc_3.getBounds((param1 as DisplayObject).parent);
                _loc_5 = _loc_3.parent.mouseX;
                _loc_6 = _loc_3.parent.mouseY;
                if (_loc_5 > _loc_4.left && _loc_5 < _loc_4.right && _loc_6 > _loc_4.top && _loc_6 < _loc_4.bottom)
                {
                    this.doDragItem(_loc_3);
                }
            }
            return;
        }// end function

        private function doDragItem(param1:DisplayObject) : void
        {
            this.clearThum();
            this.tempDragItem = param1;
            this.currentDragItem = param1 as IDragDrop;
            Global.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onDragItemMove);
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onStopDrag);
            return;
        }// end function

        private function onStopDrag(event:MouseEvent) : void
        {
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onStopDrag);
            Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onDragItemMove);
            return;
        }// end function

        private function onDragItemMove(event:Event) : void
        {
            var _loc_3:BitmapData = null;
            var _loc_4:Bitmap = null;
            var _loc_5:Rectangle = null;
            if (this.thumItem == null)
            {
                this.tempDragItem.dispatchEvent(new DragEvent(DragEvent.Event_Start_Drag, this.currentDragItem, this.currentDrogItem, this.currentDragSource, true));
                this.thumItem = new Sprite();
                if (this.dragBitMapData)
                {
                    _loc_3 = this.dragBitMapData;
                }
                else
                {
                    if (this.tempDragItem.width > 0 && this.tempDragItem.height > 0)
                    {
                        _loc_5 = this.tempDragItem.getRect(this.dragLayer);
                        _loc_3 = new BitmapData(_loc_5.width, _loc_5.height, true, 0);
                    }
                    else
                    {
                        _loc_3 = new BitmapData(1, 1, true, 0);
                    }
                    _loc_3.draw(this.tempDragItem);
                }
                _loc_4 = new Bitmap(_loc_3);
                this.thumItem.addChild(_loc_4);
                this.dragLayer.addChild(this.thumItem);
                this.thumItem.alpha = 0.7;
                this.thumItem.x = this.dragLayer.mouseX - this.tempDragItem.mouseX;
                this.thumItem.y = this.dragLayer.mouseY - this.tempDragItem.mouseY;
                this.thumItem.mouseEnabled = false;
                this.thumItem.mouseChildren = false;
                this.thumItem.startDrag();
                this.currentDragItem = this.tempDragItem as IDragDrop;
                this.tempCurrentDragItem = this.currentDragItem;
                Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onDragItemDrop);
            }
            var _loc_2:* = this.getMainContainer(this.thumItem.dropTarget);
            if (_loc_2 != this.oldMoveOverItem)
            {
                this.tempCurrentDragItem.dispatchEvent(new DragEvent(DragEvent.Event_Move_Over, this.currentDragItem, this.currentDrogItem, this.currentDragSource, true));
                if (this.oldMoveOverItem)
                {
                    this.oldMoveOverItem.dispatchEvent(new DragEvent(DragEvent.Event_Be_Drag_out, this.currentDragItem, this.currentDrogItem, this.currentDragSource, true));
                }
                if (_loc_2)
                {
                    this.currentDrogItem = _loc_2 as IDragDrop;
                    _loc_2.dispatchEvent(new DragEvent(DragEvent.Event_Be_Drag_over, this.currentDragItem, this.currentDrogItem, this.currentDragSource, true));
                    this.oldMoveOverItem = this.currentDrogItem;
                }
            }
            return;
        }// end function

        private function onDragItemDrop(event:MouseEvent) : void
        {
            var _loc_2:DisplayObject = null;
            var _loc_3:IDragDrop = null;
            this.tempDragItem.dispatchEvent(new DragEvent(DragEvent.Event_Stop_Drag, this.currentDragItem, this.currentDrogItem, this.currentDragSource, true));
            if (Global.stage.hasEventListener(MouseEvent.MOUSE_MOVE))
            {
                Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onDragItemMove);
            }
            if (this.tempCurrentDragItem != null)
            {
                _loc_2 = this.thumItem.dropTarget;
                _loc_3 = this.getMainContainer(_loc_2) as IDragDrop;
                if (_loc_3 is IDragDrop)
                {
                    if (this.canPutIn(this.currentDragItem, _loc_3))
                    {
                        this.currentDrogItem = _loc_3;
                        this.currentDragItem.dispatchEvent(new DragEvent(DragEvent.Event_Move_To, this.currentDragItem, this.currentDrogItem, this.currentDragSource, true));
                        _loc_3.dispatchEvent(new DragEvent(DragEvent.Event_Move_In, this.currentDragItem, this.currentDrogItem, this.currentDragSource, true));
                        this.thumItem.stopDrag();
                        this.clearThum();
                        this.tempCurrentDragItem = null;
                    }
                    else
                    {
                        this.thumItem.stopDrag();
                        this.moveBack();
                    }
                }
                else
                {
                    this.currentDrogItem = null;
                    if (!this.tempCurrentDragItem.isThrowAble)
                    {
                        this.thumItem.stopDrag();
                        this.moveBack();
                        this.tempCurrentDragItem = null;
                    }
                    else
                    {
                        this.thumItem.stopDrag();
                        this.clearThum();
                        this.tempCurrentDragItem.dispatchEvent(new DragEvent(DragEvent.Event_Throw_goods, this.currentDragItem, this.currentDrogItem, this.currentDragSource, true));
                        this.tempCurrentDragItem = null;
                    }
                }
            }
            else
            {
                this.clearThum();
                return;
            }
            return;
        }// end function

        private function moveBack() : void
        {
            var _loc_1:Rectangle = null;
            if (this.currentDragItem)
            {
                _loc_1 = (this.currentDragItem as DisplayObject).getBounds(this.dragLayer);
                TweenMax.to(this.thumItem, 0.2, {x:_loc_1.left, y:_loc_1.top, onComplete:this.moveBackEnd, ease:Quint.easeOut});
            }
            else
            {
                this.moveBackEnd();
            }
            return;
        }// end function

        private function moveBackEnd() : void
        {
            this.tempCurrentDragItem = null;
            this.clearThum();
            return;
        }// end function

        private function clearThum() : void
        {
            if (Global.stage.hasEventListener(MouseEvent.MOUSE_MOVE))
            {
                Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onDragItemMove);
            }
            if (Global.stage.hasEventListener(MouseEvent.MOUSE_UP))
            {
                Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onDragItemDrop);
            }
            if (this.thumItem)
            {
                if (this.dragLayer.contains(this.thumItem))
                {
                    this.dragLayer.removeChild(this.thumItem);
                }
                this.thumItem = null;
            }
            return;
        }// end function

        private function canPutIn(param1:IDragDrop, param2:IDragDrop) : Boolean
        {
            return param2.canDrop(param1, param2);
        }// end function

        private function getMainContainer(param1:DisplayObject) : DisplayObject
        {
            if (!param1)
            {
                return null;
            }
            if (param1 is IDragDrop && (param1 as IDragDrop).isDropAble)
            {
                return param1;
            }
            if (param1.parent is IDragDrop && (param1.parent as IDragDrop).isDropAble)
            {
                return param1.parent;
            }
            if (param1.parent == Global.stage)
            {
                return null;
            }
            return this.getMainContainer(param1.parent);
        }// end function

        public static function get instance() : DragManager
        {
            if (!_instance)
            {
                throw new Error("DragManager 尚未初始化");
            }
            return _instance;
        }// end function

        public static function init(param1:DisplayObjectContainer) : void
        {
            if (_instance == null)
            {
                _instance = new DragManager(param1);
            }
            return;
        }// end function

    }
}
